package com.team.teamproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.team.teamproject.R;
import com.team.teamproject.bean.Category_Colums;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 分类：栏目适配器
 */

public class ColumsAdapter extends BaseAdapter {
    private List<Category_Colums.ColumnsBean> columns;
    private Context context;

    public ColumsAdapter(Context context, List<Category_Colums.ColumnsBean> columns) {
        this.context = context;
        this.columns = columns;
    }

    @Override
    public int getCount() {
        return columns.size();
    }


    @Override
    public Object getItem(int position) {
        return columns.get(position);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.fragment_colums_item, null);
            holder = new ViewHolder(convertView);
            holder.columAuthor = (TextView) convertView.findViewById(R.id.colum_author);
            holder.columImage = (ImageView) convertView.findViewById(R.id.colum_image);
            holder.columTitle = (TextView) convertView.findViewById(R.id.colum_title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Category_Colums.ColumnsBean columnsBean = columns.get(position);
        Picasso.with(context).load(columnsBean.getBanner_image_url()).into(holder.columImage);
        holder.columTitle.setText(columnsBean.getTitle());
        holder.columAuthor.setText(columnsBean.getAuthor());
        return convertView;
    }

    static class ViewHolder {
        @InjectView(R.id.colum_image)
        ImageView columImage;
        @InjectView(R.id.colum_title)
        TextView columTitle;
        @InjectView(R.id.colum_author)
        TextView columAuthor;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
